package accs.gui;

import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.text.DateFormat;

import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.border.EmptyBorder;

import net.miginfocom.swing.MigLayout;
import accs.product.license.License;

public class LicenseDialog extends JDialog {

	private final JPanel contentPanel = new JPanel();
	private JTextField licenseKeyField;
	private JLabel expireLabel;
	private JLabel fromLabel;
	private JLabel featureLabel;

	/**
	 * Create the dialog.
	 */
	public LicenseDialog(JFrame owner) {
		super(owner,"License",true);
		setResizable(false);
		setSize(370, 225);
		getContentPane().setLayout(new BorderLayout());
		contentPanel.setBorder(new EmptyBorder(5, 5, 5, 5));
		getContentPane().add(contentPanel, BorderLayout.CENTER);
		contentPanel.setLayout(new MigLayout("", "[][grow]", "[][][][]"));
		{
			JLabel lblLicensekey = new JLabel("License-Key:");
			contentPanel.add(lblLicensekey, "cell 0 0,alignx left");
		}
		{
			licenseKeyField = new JTextField();
			licenseKeyField.setEditable(false);
			contentPanel.add(licenseKeyField, "cell 1 0,growx");
			licenseKeyField.setColumns(10);
		}
		{
			JLabel lblFrom = new JLabel("From:");
			contentPanel.add(lblFrom, "cell 0 1");
		}
		{
			fromLabel = new JLabel("?");
			contentPanel.add(fromLabel, "cell 1 1");
		}
		{
			JLabel lblExpires = new JLabel("Expires:");
			contentPanel.add(lblExpires, "cell 0 2,alignx left");
		}
		{
			expireLabel = new JLabel("?");
			contentPanel.add(expireLabel, "cell 1 2");
		}
		{
			JLabel lblNewLabel = new JLabel("Features:");
			contentPanel.add(lblNewLabel, "cell 0 3,alignx left");
		}
		{
			featureLabel = new JLabel("?");
			contentPanel.add(featureLabel, "cell 1 3");
		}
		{
			JPanel buttonPane = new JPanel();
			buttonPane.setLayout(new FlowLayout(FlowLayout.RIGHT));
			getContentPane().add(buttonPane, BorderLayout.SOUTH);
			{
				JButton okButton = new JButton("OK");
				okButton.addActionListener(new ActionListener() {
					public void actionPerformed(ActionEvent arg0) {
						dispose();
					}
				});
				okButton.setActionCommand("OK");
				buttonPane.add(okButton);
				getRootPane().setDefaultButton(okButton);
			}
		}
		
		init();
		
		pack();
		setLocationRelativeTo(null);
	}
	
	private void init() {
		License license=License.getInstance();
		
		DateFormat format=DateFormat.getDateTimeInstance(DateFormat.FULL,DateFormat.FULL);
		
		licenseKeyField.setText(license.getKey().toString());
		fromLabel.setText(format.format(license.getFromDate()));
		expireLabel.setText(format.format(license.getToDate()));
		featureLabel.setText(license.getFeatures());
	}
}
